const wxpay = require('../../utils/pay.js')
const app = getApp()
const WXAPI = require('../../wxapi/main')
Page({
  data: {
    statusType: ["待付款", "待发货", "待收货", "待评价", "已完成"],
    currentType: 0,
    tabClass: ["", "", "", "", ""],
    myOrderList:'',
    prepay_id:'',
    outTradeNo:''

  },
  toDetailsTap: function (e) {
    wx.navigateTo({
      url: "/pages/goods-details/index?id=" + e.currentTarget.dataset.id
    })
  },
  statusTap: function(e) {
    const curType = e.currentTarget.dataset.index;
    this.data.currentType = curType
    this.setData({
      currentType: curType
    });
    this.onShow();
  },
  orderDetail: function(e) {
    var orderId = e.currentTarget.dataset.id;
    wx.navigateTo({
      url: "/pages/order-details/index?id=" + orderId
    })
  },
  cancelOrderTap: function(e) {
    var that = this;
    var orderId = e.currentTarget.dataset.id;
    console.log(orderId);
    wx.showModal({
      title: '确定要取消该订单吗？',
      content: '',
      success: function(res) {
        if (res.confirm) {
          //取消订单接口
          wx.request({
            // url: con.index_getread,
            url: 'https://www.brvpmusic.com/api/orderProduct/cancelOrder',
            method: 'GET',
            data: {
              'oNumber': orderId,
            },
            header: {
              'Content-Type': 'application/json'
            },
            success: function (res) {
              console.log(res.data);
              console.log('订单取消成功');
              wx.showToast({
                title: '订单取消成功',
              })
              that.onLoad();
            },
            fail: function (res) {
              console.log('返回失败');
            }
          })
          // WXAPI.orderClose(orderId, wx.getStorageSync('token')).then(function(res) {
          //   if (res.code == 0) {
          //     that.onShow();
          //   }
          // })
        }
      }
    })
  },
  toPayTap: function(e) {
    const that = this;
    var oNumber = e.currentTarget.dataset.id;
    var money = e.currentTarget.dataset.money;
    // const needScore = e.currentTarget.dataset.score;
    console.log(e);
    wx.request({
      // url: 'https://www.brvpmusic.com/pay/hairRecharge',
      url: 'https://www.brvpmusic.com/pay/xiadan',
      data: {
        'openid': wx.getStorageSync('OpenId'),
        'money': money,
        'title': '下单支付',
      },
      method: 'GET',
      header: {
        'Content-Type': 'application/json'
      },
      success: function (res) {
        console.log(res.data);
        console.log('返回成功');
        var prepay_id = JSON.parse(res.data.body).prepay_id;
        var outTradeNo = JSON.parse(res.data.body).out_trade_no;
        that.setData({
          prepay_id: prepay_id,
          outTradeNo: outTradeNo,
        })
        // 签名地址
        wx.request({
          // url: 'https://www.brvpmusic.com/pay/hairRecharge',
          url: 'https://www.brvpmusic.com/pay/sign',
          data: {
            'prepay_id': that.data.prepay_id,
          },
          method: 'GET',
          header: {
            'Content-Type': 'application/json'
          },
          success: function (res) {
            console.log(res.data);
            console.log('返回成功');
            console.log(res.data.body)
            var timeStamp = JSON.parse(res.data.body).timeStamp;
            var nonceStr = JSON.parse(res.data.body).nonceStr;
            var packages = JSON.parse(res.data.body).package;
            var paySign = JSON.parse(res.data.body).paySign;
            // 微信支付
            wx.requestPayment({
              timeStamp: timeStamp,
              nonceStr: nonceStr,
              package: packages,
              signType: 'MD5',
              paySign: paySign,
              success(res) {
                console.log(res)
                var errMsg = res.errMsg;
                // 下单充值地址
                wx.request({
                  // url: 'https://www.brvpmusic.com/pay/hairRecharge',
                  url: 'https://www.brvpmusic.com/api/orderProduct/payment',
                  data: {
                    'oNumber': oNumber,
                    'money':money,
                    'openid': wx.getStorageSync('OpenId'),
                    'errMsg': errMsg,
                    'outTradeNo': that.data.outTradeNo
                  },
                  method: 'GET',
                  header: {
                    'Content-Type': 'application/json'
                  },
                  success: function (res) {
                    console.log(res.data);
                    console.log('返回成功');
                    wx.showToast({
                      title: '成功充值' + number + '次'
                    })
                  },
                  fail: function (res) {
                    console.log('返回失败');
                  }
                })
              },
              fail(res) {
                wx.showToast({
                  title: '您取消了充值'
                })
              }
            })
          },
          fail: function (res) {
            console.log('返回失败');
          }
        })
      },
      fail: function (res) {
        console.log('返回失败');
      }
    })
    // WXAPI.userAmount(wx.getStorageSync('token')).then(function(res) {
    //   if (res.code == 0) {
    //     // 增加提示框
    //     if (res.data.score < needScore) {
    //       wx.showToast({
    //         title: '您的积分不足，无法支付',
    //         icon: 'none'
    //       })
    //       return;
    //     }
    //     let _msg = '订单金额: ' + money +' 元'
    //     if (res.data.balance > 0) {
    //       _msg += ',可用余额为 ' + res.data.balance +' 元'
    //       if (money - res.data.balance > 0) {
    //         _msg += ',仍需微信支付 ' + (money - res.data.balance) + ' 元'
    //       }          
    //     }
    //     if (needScore > 0) {
    //       _msg += ',并扣除 ' + money + ' 积分'
    //     }
    //     money = money - res.data.balance
    //     wx.showModal({
    //       title: '请确认支付',
    //       content: _msg,
    //       confirmText: "确认支付",
    //       cancelText: "取消支付",
    //       success: function (res) {
    //         console.log(res);
    //         if (res.confirm) {
    //           that._toPayTap(orderId, money)
    //         } else {
    //           console.log('用户点击取消支付')
    //         }
    //       }
    //     });
    //   } else {
    //     wx.showModal({
    //       title: '错误',
    //       content: '无法获取用户资金信息',
    //       showCancel: false
    //     })
    //   }
    // })
  },
  _toPayTap: function (orderId, money){
    const _this = this
    if (money <= 0) {
      // 直接使用余额支付
      WXAPI.orderPay(orderId, wx.getStorageSync('token')).then(function (res) {
        _this.onShow();
      })
    } else {
      wxpay.wxpay('order', money, orderId, "/pages/order-list/index");
    }
  },
  onLoad: function(options) {
    if (options && options.type) {
      this.setData({
        currentType: options.type
      });
    }
    var that=this;
    //获取订单接口
    wx.request({
      // url: con.index_getread,
      url: 'https://www.brvpmusic.com/api/orderProduct/getListByStatus',
      method: 'GET',
      data: {
        'openid': wx.getStorageSync('OpenId'),
        'oOrderstatus':0,
      },
      header: {
        'Content-Type': 'application/json'
      },
      success: function (res) {
        console.log(res.data);
        console.log('订单返回成功');
        that.setData({
          myOrderList: res.data.body,
        })
      },
      fail: function (res) {
        console.log('返回失败');
      }
    })
  },
  onReady: function() {
    // 生命周期函数--监听页面初次渲染完成

  },
  getOrderStatistics: function() {
    var that = this;
    WXAPI.orderStatistics(wx.getStorageSync('token')).then(function(res) {
      if (res.code == 0) {
        var tabClass = that.data.tabClass;
        if (res.data.count_id_no_pay > 0) {
          tabClass[0] = "red-dot"
        } else {
          tabClass[0] = ""
        }
        if (res.data.count_id_no_transfer > 0) {
          tabClass[1] = "red-dot"
        } else {
          tabClass[1] = ""
        }
        if (res.data.count_id_no_confirm > 0) {
          tabClass[2] = "red-dot"
        } else {
          tabClass[2] = ""
        }
        if (res.data.count_id_no_reputation > 0) {
          tabClass[3] = "red-dot"
        } else {
          tabClass[3] = ""
        }
        if (res.data.count_id_success > 0) {
          //tabClass[4] = "red-dot"
        } else {
          //tabClass[4] = ""
        }

        that.setData({
          tabClass: tabClass,
        });
      }
    })
  },
  onShow: function() {
    // 获取订单列表
    var that = this;
    var postData = {
      token: wx.getStorageSync('token')
    };
    postData.status = that.data.currentType;
    this.getOrderStatistics();
    WXAPI.orderList(postData).then(function(res) {
      if (res.code == 0) {
        that.setData({
          orderList: res.data.orderList,
          logisticsMap: res.data.logisticsMap,
          goodsMap: res.data.goodsMap
        });
      } else {
        that.setData({
          orderList: null,
          logisticsMap: {},
          goodsMap: {}
        });
      }
    })
  },
  onHide: function() {
    // 生命周期函数--监听页面隐藏

  },
  onUnload: function() {
    // 生命周期函数--监听页面卸载

  },
  onPullDownRefresh: function() {
    // 页面相关事件处理函数--监听用户下拉动作

  },
  onReachBottom: function() {
    // 页面上拉触底事件的处理函数

  }
})